<?php
//include 'function.php';
class CategoryController extends Zend_Controller_Action
{
  private $db;
  public function init()
  {
    $db_obj = new myDb();
    $this->db = $db_obj->getDb();

    Zend_Db_Table::setDefaultAdapter($this->db);
  }

  public function allAction()
  {
    $mapper = new Application_Model_CategoryMapper();
    $this->view->cats = $mapper->getCategoryList(0);
  }

  /* 
   * get the sub category and courses in this category
   */
  public function selectAction()
  {
    if (!isset($_GET["cat_id"]))
      {
	die("你应该选择一个分类。");
      }
    $cat_id = $_GET["cat_id"];

    $mapper = new Application_Model_CategoryMapper();
    $cat = new Application_Model_Category();
    $mapper->find($cat_id, $cat);

    /* get the sub category list */
    $this->view->cats = $mapper->getCategoryList($cat_id);
    /* $this->view->cat_select = $cat; */
    $this->view->crumbs = $mapper->crumbAction($cat_id);
    /* print (count($this->view->crumbs)); */
    /* print_r($cat->getOptions()); */

    /* get all of gthe sub categories */
    $all_sub_cats = $mapper->getAllSubCategory($cat_id);
    $cat_str = '';
    // 计算每个等级的播放次数
    foreach($all_sub_cats as $id=>$cat)
    {
	if (!$cat_str)
		$cat_str = $id;
	else
		$cat_str .= ", $id";
	//$cat_play_times[$id] = isset($cats_play_info[$cat->get("id")]) ? $cats_play_info[$cat->get("id")] : 0;
    }
    $cat_str = "( $cat_str )";

    /* get the course with this category */
    $select = $this->db->select();
    $select->from(array("v"=>"view_history"), array("v.cont_id", "c.title", "c.thumb", "c.ori_price", "c.dis_price", "c.promotion", "times"=>"count(c.id)"))
      ->joinRight(array("c"=>"content"), "c.id = v.cont_id")
      ->where("c.cat_id in {$cat_str}")
      //->where("c.cat_id={$cat_id}")
      ->group("v.cont_id")
      ->order("times desc");
    if (isset($_GET["keywords"]) && !empty($_GET["keywords"]))
      {
	$select->where("c.title like '%".$_GET["keywords"]."%'");
      }
    $courses = $this->db->fetchAll($select);

    /* // set pageinator */
    $page = 1;
    if(isset($_GET['page']) && is_numeric($_GET['page'])){
      $page = $_GET['page'];
    }

    $paginator = Zend_Paginator::factory($courses);
    $paginator->setCurrentPageNumber($page)->setItemCountPerPage(NUM_PER_PAGE);
    $this->view->paginator = $paginator;

  }

  public function topPlayedAction()
  {
    // 播放排行榜
    $select = $this->db->select();
    $select->from(array("v"=>"view_history"), array("v.cont_id", "c.title", "c.thumb", "c.ori_price", "c.dis_price", "c.promotion", "times"=>"count(c.id)"))
      ->join(array("c"=>"content"), "c.id = v.cont_id")
      ->group("v.cont_id")
      ->order("times desc")
      ->limit(100);
    $courses = $this->db->fetchAll($select);

    $page = 1;
    if(isset($_GET['page']) && is_numeric($_GET['page'])){
      $page = $_GET['page'];
    }
    // set pageinator
    $paginator = Zend_Paginator::factory($courses);
    $paginator->setCurrentPageNumber($page)->setItemCountPerPage(NUM_PER_PAGE);
    $this->view->paginator = $paginator;
  }
  
  public function tagAction()
  {
    $cat_id = $this->getRequest()->getParam('cat_id');
    $uid = $this->getRequest()->getParam('user_id');
    $user_cond = "";
    if (!empty($uid))
      {
	$user_cond = " and c.uid=$uid ";
	$result = $this->user_table->fetchRow("id = {$uid}");
	$this->view->user = array('id' => $result->id, 'username'=> $result->username ? $result->username : $result->email);
	print_r($this->view->user);
      }
    /* get category info */
    if (!empty($cat_id))
      {
	$result = $this->category_table->fetchRow("id = {$cat_id}");
	$this->view->cat = array('id' => $cat_id, 'name'=> $result->name);
      }
    /* get tag info */
    $tag_id = $this->getRequest()->getParam('tag_id');
    $where = $this->db->quoteInto('id = ?', $tag_id);
    $this->view->tag = $this->cat_tags_table->fetchRow($where);

    /* get all courses with this tag */
    $select = $this->db->select();
    $select->from(array("v"=>"view_history"), array("v.cont_id", "c.title", "c.thumb", "c.ori_price", "c.dis_price", "c.promotion", "times"=>"count(c.id)"))
      ->join(array("c"=>"content"), "c.id = v.cont_id")
      ->join(array("t"=>"tags"), "t.cont_id = v.cont_id")
      ->where("t.tag_id={$tag_id}  {$user_cond}")
      ->group("v.cont_id")
      ->order("times desc");
    $courses = $this->db->fetchAll($select);

    /* set pageinator */
    $page = 1;
    if(isset($_GET['page']) && is_numeric($_GET['page'])){
      $page = $_GET['page'];
    }
    $paginator = Zend_Paginator::factory($courses);
    $paginator->setCurrentPageNumber($page)->setItemCountPerPage(NUM_PER_PAGE);           
    $this->view->paginator = $paginator;
  }

  public function searchAction()
  {
    //$where = 
  }

  public function pagelistAction()
  {
    Zend_Paginator::setDefaultScrollingStyle('Elastic');
    Zend_View_Helper_PaginationControl::setDefaultViewPartial('../views/scripts/search/pagelist.phtml');
    $paginator->setView($view);
  }
  public function userListAction()
  {
    $select = $this->db->select();
    $select->from(array("v"=>"view_history"), array("u.id", "u.username", "u.email", "times"=>"count(v.cont_id)"))
      ->join(array("c"=>"content"), "c.id = v.cont_id")
      ->join(array("u"=>"users"), "c.uid = u.id")
      ->group("u.id")
      ->order("times desc");
    $users = $this->db->fetchAll($select);

    /* set pageinator */
    $page = 1;
    if(isset($_GET['page']) && is_numeric($_GET['page']))
      $page = $_GET['page'];
    $paginator = Zend_Paginator::factory($users);
    $paginator->setCurrentPageNumber($page)->setItemCountPerPage(NUM_PER_PAGE);           
    $this->view->users = $paginator;

    $select = $this->db->select();
    $select->from(array("t"=>"tags"), array("c.uid", "ct.tag", "t.tag_id"))
      ->join(array("ct"=>"cat_tags"), "ct.id=t.tag_id")
      ->join(array("c"=>"content"), "c.id = t.cont_id")
      ->group(array("uid", "tag_id"));
      
    /* $sql = "select distinct(t.tag), t.uid from tags t inner join content c on t.cont_id=c.id;"; */
    $tags = array();
    foreach($this->db->fetchAll($select) as $tag)
      {
	if (!isset($tags[$tag["uid"]]))
	  $tags[$tag["uid"]] = array();
	$tags[$tag["uid"]][] = $tag;
      }
    /* print_r($tags); */
    $this->view->tags = $tags;
  }
  public function userContentListAction()
  {
    $uid = $this->getRequest()->getParam('uid');
    $tag_id = $this->getRequest()->getParam('tag_id');

    $select = $this->db->select();
    $select->from(array("v"=>"view_history"), array("v.cont_id", "c.title", "c.thumb", "c.descript", "c.ori_price", "c.dis_price", "c.promotion", "times"=>"count(v.cont_id)"))
      ->joinRight(array("c"=>"content"), "c.id = v.cont_id");
    if (!empty($tag_id))
      {
	$select->join(array("t"=>"tags"), "t.cont_id = v.cont_id")
	  ->where("t.tag_id={$tag_id}");
      }
      $select->where("c.uid={$uid}")
	->group("v.cont_id")
	->order("times desc");
    $courses = $this->db->fetchAll($select);

    /* get tags info */
    $select = $this->db->select();
    $select->from(array("t"=>"tags"), array("c.uid", "ct.tag", "t.tag_id"))
      ->join(array("ct"=>"cat_tags"), "ct.id=t.tag_id")
      ->join(array("c"=>"content"), "c.id = t.cont_id")
      ->group(array("uid", "tag_id"))
      ->where("c.uid = $uid");
    $this->view->tags = $this->db->fetchAll($select);

    // set pageinator
    $page = 1;
    if(isset($_GET['page']) && is_numeric($_GET['page'])){
      $page = $_GET['page'];
    }
    $paginator = Zend_Paginator::factory($courses);
    $paginator->setCurrentPageNumber($page)->setItemCountPerPage(NUM_PER_PAGE);
    $this->view->paginator = $paginator;

    // get user info
    $user_table = new Application_Model_DbTable_User();
    $where = $this->db->quoteInto('id = ?', $uid);
    $user = $user_table->fetchRow($where);  
    $this->view->user = array('uid'=>$user['id'],
			      'username'=>$user['username'],
			      'email'=>$user['email'],
			      'realname'=>$user['realname']);

  }

}
